GMMMPEC_hess_ktr_FE <- function(x,obj_factor,hessian_lambda) { 
  
 
x<-as.matrix(x)

theta1 <- x[1:(K_A+1), 1]     
theta2 <- x[((K_A+1)+1):((nD+1)*(K_A+1) + (countries-1) + nD),1] 
betaf <- theta2[(nD*(K_A+1)+1):(length(theta2)-nD)] 
sigmaf <- theta2[(length(theta2)-nD+1):length(theta2)]
delta = x[((nD+1)*(K_A+1) + (countries-1) + nD + 1):((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal), 1]
g = x[((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal +1):length(theta2), 1] 
g <- as.matrix(g) 


nx<-length(x)
ng<-length(g)
ooo<-matrix(1,1,K_A+1)
ooo2<-matrix(1,1,nD)
ooo3 <- matrix(1, countries-1, 1)


hess<-matrix(0,nx,nx)
hess[((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal+1):nrow(hess), ((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal+1):ncol(hess)] = 2*W



expmu = matrix(0,nrow(x_char),ncol(v))

for (t in 1:T) { 
theta2_mat = rbind(as.matrix(theta2[1:(K_A+1)]), theta2[length(theta2)-nD+1]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))
expmu[marketStarts[t,1]:marketEnds[t,1],] = exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[1:(K+1),,t] + x_char[marketStarts[t,1]:marketEnds[t,1],]%*% rbind(matrix(0,K_A+1,1), as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))%*%matrix(1,1,nn))   
for (dd in 2:nD) {
theta2_mat = rbind(as.matrix(theta2[((dd-1)*(K_A+1)+1):(dd*(K_A+1))]), theta2[length(theta2)-nD+dd]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))   
expmu[marketStarts[t,1]:marketEnds[t,1],] = expmu[marketStarts[t,1]:marketEnds[t,1],] * exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[((dd-1)*(K+1)+1):(dd*(K+1)),,t]) 
}
}


expmeanval = as.matrix(exp(delta))
simEstShare<-ind_shnormMPEC(expmeanval,expmu, oo, sharesum, marketForProducts)
simShare<-simEstShare[,1:(ncol(simEstShare)-1)]
EstShare<-simEstShare[,ncol(simEstShare)]
EstShare<-as.matrix(EstShare)

 
dL2dtheta2 = matrix(0, (nD*(K_A+1)), (nD*(K_A+1)))
dL2dtheta2rr = matrix(0, (nD*(K_A+1)), (nD*(K_A+1)))
dL2dsigmaf2 = matrix(0,nD,nD)
dL2dsigmaf2rr = matrix(0,nD,nD)
dL2dbetaf2 = matrix(0, countries-1, countries-1)
dL2dtheta2dsigmaf = matrix(0, nD, nD*(K_A+1))
dL2ddeltadtheta2 = matrix(0, numProdsTotal, nD*(K_A+1))
dL2ddeltadbetaf = matrix(0, numProdsTotal, countries-1)
dL2ddeltadsigmaf = matrix(0, numProdsTotal, nD)
dL2ddelta2DIAG = matrix(0, numProdsTotal,prods)
dL2ddelta2 = matrix(0, numProdsTotal, numProdsTotal)
dL2dbetafdtheta2 = matrix(0, countries-1,nD*(K_A+1))
dL2dbetafdsigmaf = matrix(0, countries-1,nD)


lambdaeqnonlin=hessian_lambda
lambdaeqnonlin<-as.matrix(lambdaeqnonlin)


for (t in 1:T) {
index<-(marketStarts[t]:marketEnds[t])
index_start <- marketStarts[t]
index_end <- marketEnds[t]
multip = lambdaeqnonlin[index]
multip<-as.matrix(multip)
ooo1<-matrix(1,prodsMarket[t],1)
prodsMarket_t <- prodsMarket[t]
x_char_index<-x_char[index,]
b_market = x_char[index,(K_A+2):ncol(x_char)]%*%as.matrix(betaf)
v_market = array(0, dim=c(prodsMarket[t],nn,nD))
for (dd in 1:nD) {
v_market[,,dd] = x_char[index,(K_A+2):ncol(x_char)]%*%v[((dd-1)*(K+1)+ (K_A+1) + 1):(dd*(K+1)),,t]
}
v_market_v = v_market[,,1]
v_market_Y = v_market[,,2]

List <- blk_cpp_FE( simShare, multip, nn, dL2ddelta2DIAG, dL2ddeltadtheta2, dL2dtheta2, dL2ddeltadbetaf, dL2dbetaf2, dL2ddeltadsigmaf, dL2dbetafdtheta2, dL2dbetafdsigmaf, dL2dsigmaf2, dL2dtheta2dsigmaf, index_start, index_end, x_char_index, x_char, as.matrix(betaf), as.matrix(sigmaf), v_market_v, v_market_Y, b_market, ooo1, ooo3, v[,,t], K, K_A, nD, prodsMarket_t, t ) 

dL2ddelta2DIAG = List$dL2ddelta2DIAG_new 
dL2ddeltadtheta2 = List$dL2ddeltadtheta2_new
dL2dtheta2 = List$dL2dtheta2_new
dL2ddeltadbetaf = List$dL2ddeltadbetaf_new
dL2dbetaf2 = List$dL2dbetaf2_new
dL2ddeltadsigmaf = List$dL2ddeltadsigmaf_new
dL2dbetafdtheta2 = List$dL2dbetafdtheta2_new
dL2dbetafdsigmaf = List$dL2dbetafdsigmaf_new
dL2dsigmaf2 = List$dL2dsigmaf2_new
dL2dtheta2dsigmaf = List$dL2dtheta2dsigmaf_new
  
dL2ddelta2[index,index] = dL2ddelta2DIAG[index,1:prodsMarket[t]]    
}

hess = hess_cpp_FE(hess, dL2dtheta2, dL2ddeltadtheta2, dL2ddelta2, dL2dbetaf2, dL2dbetafdtheta2, dL2dbetafdsigmaf, dL2ddeltadbetaf, dL2dsigmaf2, dL2dtheta2dsigmaf, dL2ddeltadsigmaf, nD, K, K_A, countries, numProdsTotal)  


hess_tri_vector<-hess[index_Hessian]

return(hess_tri_vector)

}



